package org.fastsyncer.web.controller;

import org.fastsyncer.biz.ConnectorService;
import org.fastsyncer.biz.MappingService;
import org.fastsyncer.web.remote.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/test")
public class TestController {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private ConnectorService connectorService;

	@Autowired
	private MappingService mappingService;

    @Autowired
	private UserService userService;

	@GetMapping("")
	public String index(HttpServletRequest request, ModelMap model) {
		return "test.html";
	}

	@RequestMapping("/demo")
	@ResponseBody
	public Object demo(Model model) {
		logger.info("demo");

        String res = userService.hello("我是master");
        logger.info("slave响应:{}", res);

        // TODO 测试
//		testAlive();
//		String id = testConnctorSave();
//		testConnctorRemove(id);

        //id = testMappingrSave();
//        testMappingRemove(id);

        return "hello";
	}

    private void testMappingRemove(String id) {
        mappingService.remove(id);
    }

    private String testMappingrSave() {
        String json = "{\"name\":\"人力资源系统_审计系统\",\"sourceConnectorId\":\"1\",\"targetConnectorId\":\"1\",\"tableGroup\":[\"tableGroupId_0\",\"tableGroupId_1\",\"tableGroupId_999\"],\"filter\":{\"and\":[{\"name\":\"AGE\",\"operator\":\"gt\",\"value\":\"0\"}],\"or\":[{\"name\":\"NAME\",\"operator\":\"equal\",\"value\":\"hello\"}]},\"fieldConvert\":[{\"name\":\"NAME\",\"convert\":[{\"name\":\"append\",\"args\":[\"hello\"]}]}],\"plugin\":{\"className\":\"com.xx.hr.convert.MyImpl\"},\"listener\":{\"listenerType\":\"Polling\",\"cronExpression\":\"*/20 * * * * ? *\",\"eventFieldName\":\"MyEvent\",\"update\":[\"U\",\"M\"],\"insert\":[\"I\"],\"delete\":[\"D\"]},\"maxNum\":10000,\"batchNum\":200,\"threadNum\":5,\"createDateTime\":\"2019-10-08 21:32:00\",\"updateDateTime\":\"2019-10-08 21:35:00\"}";
        return mappingService.save(json);
    }

    private void testConnctorRemove(String id) {
		connectorService.remove(id);
	}

	private String testConnctorSave() {
		String json = "{\"id\":1,\"name\":\"人力资源系统\",\"config\":{\"connectorType\":\"Mysql\",\"driverClassName\":\"com.mysql.jdbc.Driver\",\"url\":\"jdbc:mysql://127.0.0.1:3306/test?seUnicode=true&characterEncoding=UTF8&useSSL=true\",\"primaryKey\":\"ID\",\"username\":\"root\",\"password\":\"123\"}}";
		return connectorService.save(json);
	}

	private void testAlive() {
		String json = "{\"connectorType\":\"Mysql\",\"driverClassName\":\"com.mysql.jdbc.Driver\",\"url\":\"jdbc:mysql://127.0.0.1:3306/test?seUnicode=true&characterEncoding=UTF8&useSSL=true\",\"primaryKey\":\"ID\",\"username\":\"root\",\"password\":\"123\"}";
		connectorService.alive(json);
	}

}
